package com.intro.database.dao;

import javax.persistence.Entity;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.intro.database.entity.News;
import com.intro.util.HibernateSessionFactory;

@Entity
public class NewsDao {
	public boolean save(News transientInstance) {
		//log.debug("saving Account instance");
		Session session = HibernateSessionFactory.getSession();
		Transaction tr = session.beginTransaction();
		try {
			tr.begin();
			session.save(transientInstance);
			tr.commit();
			//log.debug("save successful");
		} catch (Exception re) {
			re.printStackTrace();
			//log.error("save failed", re);
			if(tr != null) {
				tr.rollback();
			}
			return false;
		}
		return true;
	}
	
	public boolean delete(News persistentInstance) {
		//log.debug("deleting Account instance");
		Session session = HibernateSessionFactory.getSession();
		Transaction tr = session.beginTransaction();
		try {
			tr.begin();
			session.delete(persistentInstance);
			tr.commit();
			//log.debug("delete successful");
		} catch (Exception re) {
			//log.error("delete failed", re);
			if(tr != null) {
				tr.rollback();
			}
			return false;
		}
		return true;
	}
	
	public News updateNews(News news) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tr = session.beginTransaction();
		try {
			tr.begin();
			session.update(news);
			tr.commit();
			//log.debug("delete successful");
		} catch (Exception re) {
			//log.error("delete failed", re);
			if(tr != null) {
				tr.rollback();
			}
			return null;
		}
		return news;
	}
}
